package com.qf.forum.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.base.forum.IconDTO;
import com.qf.base.push.MessageDTO;
import com.qf.base.result.R;
import com.qf.base.utils.FileBase64Util;
import com.qf.base.utils.SnowFlakeUtil;
import com.qf.forum.client.FileClient;
import com.qf.forum.client.PushClient;
import com.qf.forum.dao.ForumDao;
import com.qf.forum.dao.ForumDaoMapper;
import com.qf.forum.dao.ForumUserDao;
import com.qf.forum.dao.ForumUserDaoMapper;
import com.qf.forum.dto.*;
import com.qf.forum.model.*;
import com.qf.forum.service.ForumService;
import org.mockito.internal.util.StringUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class ForumServiceimpl implements ForumService {
    @Resource
    private ForumDao forumDao;
    @Resource
    private ForumDaoMapper forumDaoMapper;
    @Resource
    private ForumUserDaoMapper forumUserDaoMapper;
    @Resource
    private FileClient fileClient;
    @Resource
    private ForumUserDao forumUserDao;
    @Resource
    private PushClient pushClient;
    @Override
    public boolean insert(Long userId, ForumDTO forumDTO) {
        if (StringUtils.isEmpty(userId)){
            return false;
        }
        String title = forumDTO.getTitle();
        String content = forumDTO.getContent();
        if (StringUtils.isEmpty(title)){
            return false;
        }
        if (StringUtils.isEmpty(content)){
            return false;
        }
//        ForumExample forumExample=new ForumExample();
//        ForumExample.Criteria criteria = forumExample.createCriteria();
//        criteria.andUidEqualTo(userId);
//        criteria.andTitleEqualTo(title);
//
//        List<Forum> forums = forumDao.selectByExample(forumExample);
//        if (forums!=null&&forums.size()>0){
//            return false;
//        }
        String s="";
        if (forumDTO.getState()==1){
            String ss = forumDTO.getContent();
            String[] split = ss.split(" ");
            if (split.length>9){
                return false;
            }
            IconDTO iconDTO=new IconDTO();
            for (int i=0;i<split.length;i++) {
                iconDTO.setBase64(split[i]);
                String s1 = fileClient.saveImage(iconDTO);
                JSONObject jsonObject = JSONObject.parseObject(s1);
                String o = (String) jsonObject.get("result");
                if (s=="") {
                    s=s+""+o;
                }else {
                    s=s+","+o;
                }
            }
            Forum forum=new Forum();
            forum.setUid(userId);
            forum.setTitle(title);
            forum.setContent(s);
            forum.setType(forumDTO.getType());
            forum.setState(forumDTO.getState());
            int result = forumDao.insertSelective(forum);
            if (result<1){
                return false;
            }
        }else {
            Forum forum=new Forum();
            forum.setUid(userId);
            forum.setTitle(title);
            forum.setContent(content);
            forum.setType(forumDTO.getType());
            forum.setState(forumDTO.getState());
            int result = forumDao.insertSelective(forum);
            if (result<1){
                return false;
            }
        }
        return true;
    }
    @Override
    public boolean insert(Long userId, MultipartFile file, ForumUrlDTO forumUrlDTO) {
        if (StringUtils.isEmpty(userId)){
            return false;
        }
        String title = forumUrlDTO.getTitle();
        if (StringUtils.isEmpty(title)){
            return false;
        }
        if (StringUtils.isEmpty(file)){
            return false;
        }

        if (forumUrlDTO.getState()==2){
            String s = fileClient.uploadMP4newGif(file);
            JSONObject jsonObject = JSONObject.parseObject(s);
            String o = (String) jsonObject.get("result");
            Forum forum=new Forum();
            forum.setUid(userId);
            forum.setTitle(title);
            forum.setContent(o);
            forum.setType(forumUrlDTO.getType());
            forum.setState(forumUrlDTO.getState());
            int result = forumDao.insertSelective(forum);
            if (result<1){
                return false;
            }
        }
        return true;
    }
//查询前五条数据
    @Override
    public R selectforum(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<Forum> select = forumDaoMapper.select();
        if (select==null&&select.size()<1){
            return R.ok(200,"未找到任何数据");
        }
        PageInfo pageInfo=new PageInfo(select);
        return R.ok(pageInfo.getList(),pageInfo.getTotal());
    }

    @Override
    public R selecforumuser(ForumUserDTO forumUserDTO) {
        Forum forum = forumDao.selectByPrimaryKey(forumUserDTO.getId());
        if (StringUtils.isEmpty(forum)){
            return R.error(500,"未找到数据，请稍后重试");
        }
        //有评论
        List<ForumVO> result = forumUserDaoMapper.select(forumUserDTO.getId());
        if (result==null&&result.size()<1){
            return R.ok(200,"查询成功").setResult(forum);
        }

        return R.ok(200,"查询成功").setResult(result);
    }

    @Override
    public R insertforumuser(Long userId, ForumInsertUserDTO forumInsertUserDTO) {
        Forum forum = forumDao.selectByPrimaryKey(forumInsertUserDTO.getId());
        if (StringUtils.isEmpty(forum)){
            return R.error(500,"该论坛未找到任何数据，请稍后重试");
        }
        //有论坛的基础下,根据当前用户来进行评论
        ForumUser forumUser=new ForumUser();
        forumUser.setFid(forum.getUid());
        forumUser.setUid(userId);
        forumUser.setCon(forumInsertUserDTO.getCon());
        int i = forumUserDao.insertSelective(forumUser);
        if (i!=1){
            return R.error(500,"添加论坛信息失败");
        }
        //当用户上传成功后，需要看到自己的信息
        MessageDTO messageDTO=new MessageDTO();
        messageDTO.setUid(userId);
        messageDTO.setType(4);
        messageDTO.setMessage(forumInsertUserDTO.getCon());
        messageDTO.setRequestId(new SnowFlakeUtil().nextId());
        pushClient.send(messageDTO);
        return R.ok(200,"添加论坛信息成功");
    }


}
